package com.efast.cafe.commponent.crud.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.cafe.commponent.crud.service.ICrudV2Service;
import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.controller.AbstractController;
import com.efast.cafe.framework.enums.ResultStatus;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.portal.bean.common.AttachmentFormBean;
import com.efast.cafe.portal.entity.common.PortalAttachmentForm;
import com.efast.cafe.portal.entity.common.PortalMultiEntityBean;
import com.efast.cafe.util.StringUtil;

@Controller
@RequestMapping("/dataService_v2")
public class CrudV2Controller extends AbstractController {

	@Autowired
	private ICrudV2Service crudService;

	@SuppressWarnings({ "unchecked", "deprecation" })
	@RequestMapping(value = "/{tableName}/insert")
	public @ResponseBody ResultBean crudInsert(@PathVariable String tableName, @RequestBody Map<String, Object> map) {
		ResultBean res = new ResultBean(true, "");
		Integer resId = null;
		try {
			if(StringUtil.isNullOrBlank(tableName)){
				throw new ServiceException(ResultStatus.PARAM_ERROR.getCode());
			}
			tableName = cleanXSS(tableName);
			if (map.get("attachments") != null) {

				AttachmentFormBean attachments = new AttachmentFormBean();
				net.sf.json.JSONArray jry = net.sf.json.JSONArray.fromObject(map.get("attachments"));
				List<PortalAttachmentForm> list = net.sf.json.JSONArray.toList(jry, PortalAttachmentForm.class);
				attachments.setAttachments(list);
				resId = crudService.crudInsert(tableName, map, attachments);
			} else {
				resId = crudService.crudInsert(tableName, map, null);
			}
			res.setObject(resId);
		} catch (Exception e) {
			e.printStackTrace();
			res.setSuccess(false);
			if (e instanceof ServiceException) {
				res.setErrorCode(((ServiceException) e).getCode());
				res.setMsgParams(((ServiceException) e).getParams());
			}else {
				res.setErrorCode(ResultStatus.SYS_ERROR.getCode());
			}
		}
		return res;
	}
	
	@RequestMapping(value = "/{tableName1}/{tableName2}/insert")
	public @ResponseBody ResultBean crudInserts(@PathVariable String tableName1,@PathVariable String tableName2,
			@RequestBody List<Map<String, Object>> list){
		ResultBean res = new ResultBean(true, "");
		try {
			if(StringUtil.isNullOrBlank(tableName1) || StringUtil.isNullOrBlank(tableName2)){
				throw new ServiceException(ResultStatus.PARAM_ERROR.getCode());
			}
			tableName1 = cleanXSS(tableName1);
			tableName2 = cleanXSS(tableName2);
			List<Map<String,Object>> dataMap = new ArrayList();
			if(list!=null && !list.isEmpty() && list.size()==2){
				for(int i=0;i<list.size();i++){
					Map<String, Object> map = list.get(i);
					if (map.get("attachments") != null) {
						AttachmentFormBean attachments = new AttachmentFormBean();
						net.sf.json.JSONArray jry = net.sf.json.JSONArray.fromObject(map.get("attachments"));
						List<PortalAttachmentForm> attachmentList = net.sf.json.JSONArray.toList(jry, PortalAttachmentForm.class);
						attachments.setAttachments(attachmentList);
						map.put("dataService_crud_attachmentList", attachments);
					}
					if(i==0)
						map.put("dataService_crud_entityName", tableName1);
					else
						map.put("dataService_crud_entityName", tableName2);
					dataMap.add(map);
				}
				crudService.crudInsert(dataMap.get(0),dataMap.get(1));
			}
		} catch (Exception e) {
			e.printStackTrace();
			res.setSuccess(false);
			if (e instanceof ServiceException) {
				res.setErrorCode(((ServiceException) e).getCode());
				res.setMsgParams(((ServiceException) e).getParams());
			}else {
				res.setErrorCode(ResultStatus.SYS_ERROR.getCode());
			}
		}
		
		return res;
	}
	
	
	
	@RequestMapping(value = "/{entityName1}/{entityName2}/updateByPrimaryKey")
	public @ResponseBody ResultBean crudUpdates(@PathVariable String entityName1,@PathVariable String entityName2, 
			@RequestBody List<Map<String, Object>> list) {
		ResultBean res = new ResultBean(true, "");
		try {
			if(StringUtil.isNullOrBlank(entityName1) || StringUtil.isNullOrBlank(entityName2)){
				throw new ServiceException(ResultStatus.PARAM_ERROR.getCode());
			}
			entityName1 = cleanXSS(entityName1);
			entityName2 = cleanXSS(entityName2);
			List<Map<String,Object>> dataMap = new ArrayList();
			if(list!=null && !list.isEmpty()){
				for(int i=0;i<list.size();i++){
					Map<String, Object> map = list.get(i);
					if (map.get("attachments") != null) {
						AttachmentFormBean attachments = new AttachmentFormBean();
						net.sf.json.JSONArray jry = net.sf.json.JSONArray.fromObject(map.get("attachments"));
						List<PortalAttachmentForm> attachmentList = net.sf.json.JSONArray.toList(jry, PortalAttachmentForm.class);
						attachments.setAttachments(attachmentList);
						map.put("dataService_crud_attachmentList", attachments);
					}
					if(i==0)
						map.put("dataService_crud_entityName", entityName1);
					else
						map.put("dataService_crud_entityName", entityName2);
					dataMap.add(map);
				}
				crudService.crudUpdate(dataMap);
			}
		} catch (Exception e) {
			e.printStackTrace();
			res.setSuccess(false);
			if (e instanceof ServiceException) {
				res.setErrorCode(((ServiceException) e).getCode());
				res.setMsgParams(((ServiceException) e).getParams());
			} else {
				res.setErrorCode(ResultStatus.SYS_ERROR.getCode());
			}
		}
		return res;
	}
	
	
	

	@RequestMapping(value = "/{entityName}/updateByPrimaryKey")
	public @ResponseBody ResultBean crudUpdate(@PathVariable String entityName, @RequestBody Map<String, Object> map) {
		ResultBean res = new ResultBean(true, "");
		try {
			if(StringUtil.isNullOrBlank(entityName)){
				throw new ServiceException(ResultStatus.PARAM_ERROR.getCode());
			}
			entityName = cleanXSS(entityName);
			if (map.get("attachments") != null) {
				AttachmentFormBean attachments = new AttachmentFormBean();
				net.sf.json.JSONArray jry = net.sf.json.JSONArray.fromObject(map.get("attachments"));
				List<PortalAttachmentForm> list = net.sf.json.JSONArray.toList(jry, PortalAttachmentForm.class);
				attachments.setAttachments(list);
				crudService.crudUpdate(entityName, map, attachments);
			} else {
				crudService.crudUpdate(entityName, map, null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			res.setSuccess(false);
			if (e instanceof ServiceException) {
				res.setErrorCode(((ServiceException) e).getCode());
				res.setMsgParams(((ServiceException) e).getParams());
			} else {
				res.setErrorCode(ResultStatus.SYS_ERROR.getCode());
			}
		}
		return res;
	}

	@RequestMapping(value = "/{entityName}/deleteByPrimaryKey")
	public @ResponseBody ResultBean crudDel(@PathVariable String entityName, @RequestBody Map<String, Object> map) {
		ResultBean res = new ResultBean(true, "");
		try {
			if(StringUtil.isNullOrBlank(entityName)){
				throw new ServiceException(ResultStatus.PARAM_ERROR.getCode());
			}
			entityName = cleanXSS(entityName);
			crudService.crudDelete(entityName, map);
		} catch (Exception e) {
			e.printStackTrace();
			res.setSuccess(false);
			if (e instanceof ServiceException) {
				res.setErrorCode(((ServiceException) e).getCode());
				res.setMsgParams(((ServiceException) e).getParams());
			} else {
				res.setErrorCode(ResultStatus.SYS_ERROR.getCode());
			}
		}
		return res;
	}

	@RequestMapping(value = "/{entityName}/findByPrimaryKey")
	public @ResponseBody ResultBean crudFindById(@PathVariable String entityName, @RequestBody Map<String, Integer> map) {
		ResultBean res = new ResultBean(true, "");
		try {
			if(StringUtil.isNullOrBlank(entityName)){
				throw new ServiceException(ResultStatus.PARAM_ERROR.getCode());
			}
			entityName = cleanXSS(entityName);
			Object obj = crudService.crudFindById(entityName, map);
			res.setObject(obj);
		} catch (Exception e) {
			e.printStackTrace();
			res.setSuccess(false);
			if (e instanceof ServiceException) {
				res.setErrorCode(((ServiceException) e).getCode());
				res.setMsgParams(((ServiceException) e).getParams());
			} else {
				res.setErrorCode(ResultStatus.SYS_ERROR.getCode());
			}
		}
		return res;
	}

	
	@RequestMapping(value = "/{entityName}/multiEntityInsert")
	public @ResponseBody ResultBean multiEntityInsert(@PathVariable String entityName, @RequestBody PortalMultiEntityBean portalMultiEntityBean) {
		ResultBean res = new ResultBean(true, "");
		if (portalMultiEntityBean == null) {
			res.setSuccess(false);
			res.setErrorCode(ResultStatus.PARAM_ERROR.getCode());
			return res;
		}
		try {
			if(StringUtil.isNullOrBlank(entityName)){
				throw new ServiceException(ResultStatus.PARAM_ERROR.getCode());
			}
			entityName = cleanXSS(entityName);
			crudService.multiEntityInsert(entityName, portalMultiEntityBean);
		} catch (Exception e) {
			e.printStackTrace();
			res.setSuccess(false);
			if (e instanceof ServiceException) {
				res.setErrorCode(((ServiceException) e).getCode());
				res.setMsgParams(((ServiceException) e).getParams());
			} else {
				res.setErrorCode(ResultStatus.SYS_ERROR.getCode());
			}
		}
		return res;
	}
	
	
}
